
import axios from "axios";
import Qs from "qs";

const instance = axios.create({
  // baseURL:"http://fc.php800.cn/api",
    headers:{
        "Content-Type":"application/x-www-form-urlencoded"
    },
    transformRequest : [function (data) {
      let params = null;
      if(data instanceof FormData || data instanceof URLSearchParams){
        params = data
      }else{
        params = Qs.stringify(data)
      }
      return params
  }],
  transformResponse : [function (data) {
    // console.log(data);
    // console.log(JSON.parse(data));
    return JSON.parse(data);
}]
}) 

instance.interceptors.request.use(function (config) {
  if(localStorage.getItem("userData")){
    config.headers.token =JSON.parse(localStorage.getItem("userData")).data.token
    config.headers.phone =JSON.parse(localStorage.getItem("userData")).data.phone
  }

  //#开头默认本地文件
  if(config.url.startsWith("#")){
    config.baseURL = config.url.substring(1)
  }
  //  console.log(config);
    return config;
  }, function (error) {

    return Promise.reject(error);
  });


instance.interceptors.response.use(function (res) {
    // console.log(res.data);
    return res.data;
  }, function (error) {
    return Promise.reject(error);
  });

export default instance